﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Transactions;
using IssueTracker.Data;
using IssueTracker.Business.Entity;

namespace IssueTracker.Business.BO
{
    public class IssueTaskBO
    {

  
        private IssueTaskDataAccess da;

        public IssueTaskBO() 
        {
            da = new IssueTaskDataAccess();
        }


#region Business Methods

        /// <summary>
        /// Add a new IssueTask
        /// </summary>
        /// <param name="issueTask"></param>
        /// <returns></returns>
        public IssueTask AddNew(IssueTask issueTask)
        {
            IssueTask returnIssueTask;

            issueTask.CreationDate = DateTime.Today.Date;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnIssueTask = da.Create(issueTask);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }

            return returnIssueTask;

        }

        public IssueTask UpdateIssueTask(IssueTask issueTask)
        {
            IssueTask returnIssueTask;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnIssueTask = da.Update(issueTask);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                
                return returnIssueTask;
            }
        }

        public void DeleteIssueTask(IssueTask issueTask)
        {
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    da.Delete(issueTask);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<IssueTask> ListIssueTasks()
        {
            IssueTaskDataAccess data = new IssueTaskDataAccess();

            IEnumerable<IssueTask> returnList = data.List();
            return returnList;
        }

        public IssueTask GetIssueTask(System.Guid id)
        {
            return da.Retrieve(id);
        }

        public IEnumerable<IssueTask> ListIssueTasksByRef(System.Guid reference)
        {
            return da.ListByReferenceId(reference);
        }
#endregion
        
   
    }
}
